//
//  LLRowPickLogic.m
//  JoySpell
//
//  Created by linl on 2018/3/26.
//  Copyright © 2018年 xkdao. All rights reserved.
//

#import "M1RowPicker.h"
#import "M1RowPickView.h"

#define m1_NULLString(string) ((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)

@interface M1RowPicker ()
{
    BOOL isNew;
}

@end

@implementation M1RowPicker


#pragma mark - 获取指定文本属性集
- (NSMutableAttributedString *)m1_attributedStr:(NSString *)text selectText:(id)selectText selectAttr:(id)selectAttr
{
    NSMutableAttributedString *atStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    if ([selectText isKindOfClass:[NSString class]]) {
        NSString *str = selectText;
        NSRange range = [text rangeOfString:str];
        [atStr addAttributes:selectAttr range:range];
    }
    
    if ([selectText isKindOfClass:[NSArray class]]) {
        NSArray *array = selectText;
        for (int i = 0; i < array.count; i++) {
            NSRange range = [text rangeOfString:array[i]];
            [atStr addAttributes:selectAttr[i] range:range];
        }
    }
    
    return atStr;
}


#pragma mark - textField委托处理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    return YES;
}

- (void)textFieldDidChange:(UITextField *)textField
{
   
    NSString *str = [self m1_stringRemoveEmoji:textField.text];
    if (![str isEqualToString:textField.text])
    {
        textField.text = str;
    }

}

- (NSString *)m1_stringRemoveEmoji:(NSString *)text
{
    __block BOOL returnValue = NO;
    
    __block NSMutableString *returnString = [[NSMutableString alloc] init];
    
    [text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
     {
         NSString *pattern = @"^[\\s\u4e00-\u9fa5a-zA-Z0-9\\p{P}+~$`^=|<>～｀＄＾＋＝｜＜＞￥×£€]+$";
         
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
         
         returnValue = [predicate evaluateWithObject:substring];
         
         if (returnValue)
         {
             [returnString appendString:substring];
         }
     }];
    
    return returnString;
}

@end
